//
//  WBNewFeatureController.swift
//  weibo
//
//  Created by Felix on 16/11/15.
//  Copyright © 2016年 Felix. All rights reserved.
//

import UIKit
import SnapKit

class WBNewFeatureController: UIViewController {

    var maxCount:Int = 4
    override func viewDidLoad() {
        super.viewDidLoad()
    }

}



extension WBNewFeatureController:UICollectionViewDataSource{

     func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxCount
    }
    
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "WBNewFrature", for: indexPath)as!WBNewFeatureCell
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.red:UIColor.orange
        cell.index = indexPath.item
        return cell
    }
}



extension WBNewFeatureController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath){
        let index = collectionView.indexPathsForVisibleItems.last
        let currentCell = collectionView.cellForItem(at: index!)as!WBNewFeatureCell
        
        if index?.item == (maxCount-1) {
            currentCell.startAnimation()
        }
        
    }
}


//MARK:- WBCollectionViewCell
class WBNewFeatureCell:UICollectionViewCell{
    var index:Int = 0
    {
        didSet{
            iconImageView.image = UIImage.init(named: "new_feature_\(index + 1)")
            startButton.isHidden = true
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUI()
    }
    
    private func setUI(){
        contentView.addSubview(iconImageView)
        contentView.addSubview(startButton)
        iconImageView.snp.makeConstraints { (maker) in
            maker.edges.equalToSuperview()
        }
        
        startButton.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.bottom.equalToSuperview().offset(-160)
        }
    }
    
    
    func startAnimation(){
        startButton.isHidden = false
        startButton.isUserInteractionEnabled = false
        startButton.transform = CGAffineTransform.init(scaleX: 0.0, y: 0.0)
       UIView.animate(withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions.allowAnimatedContent, animations: { 
         self.startButton.transform = CGAffineTransform.identity
        }) { (true) in
            self.startButton.isUserInteractionEnabled = true
        }
        
   
    }
    
    
    //1.图片
    lazy var iconImageView = UIImageView()
    
    //2.开始按钮
    lazy var startButton:UIButton = {
        
        let button = UIButton(imageName: "", backgroundImageName: "new_feature_button")
        button.addTarget(self, action: #selector(startButtonClick), for: UIControlEvents.touchUpInside)
        return button
        
    }()
    
    
    @objc private func startButtonClick(){
        NotificationCenter.default.post(name: NSNotification.Name.init(WBSwitchRootViewController), object: true)
    }
}


///定义布局
class WBNewFeatureViewFlowLayout: UICollectionViewFlowLayout {
    //1.准备布局
    override func prepare() {
        //1.1设置每个cell的尺寸
        itemSize = UIScreen.main.bounds.size
        
        //1.2设置每个cell之间的间隙
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        
        //1.3设置滚动放向
        scrollDirection = UICollectionViewScrollDirection.horizontal
        
        //1.4设置为可分页
        collectionView?.isPagingEnabled = true
        
        //1.5设置为不可回弹
        collectionView?.bounces = false
        
        //1.6隐藏滚动条
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        
    }
}



